import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { JWT_LS } from '../constants'

interface IAuthState {
  token: string
}

const initialState: IAuthState = {
  token: localStorage.getItem(JWT_LS) || ''
}

export const authSlice = createSlice({
  name: 'auth',
  initialState,
  reducers: {
    setToken(state, action: PayloadAction<string>) {
      state.token = action.payload
      localStorage.setItem(JWT_LS, action.payload)
    }
  }
})

export const authActions = authSlice.actions
export const authReducer = authSlice.reducer
